package com.david.zhufengfm.Fragments;


import android.os.Bundle;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;

import com.david.zhufengfm.Fragments.discover.DiscoverAnchorFragment;
import com.david.zhufengfm.Fragments.discover.DiscoverCategoryFragment;
import com.david.zhufengfm.Fragments.discover.DiscoverLiveFragment;
import com.david.zhufengfm.Fragments.discover.DiscoverRecommendFragment;
import com.david.zhufengfm.R;
import com.david.zhufengfm.adapters.CommonFragmentAdapter;

import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverFragment extends Fragment implements TabLayout.OnTabSelectedListener {


    private ViewPager pager;

    public DiscoverFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_doscover, container, false);
        TabLayout tabLayout= (TabLayout) view.findViewById(R.id.discover_tab_bar);
//        //1.创建tab
//
//        TabLayout.Tab tab = tabLayout.newTab().setText("推荐");
//        tabLayout.addTab(tab);
//        tab=tabLayout.newTab().setText("分类");
//        tabLayout.addTab(tab);
//        tab=tabLayout.newTab().setText("直播");
//        tabLayout.addTab(tab);
//        tab=tabLayout.newTab().setText("榜单");
//        tabLayout.addTab(tab);
//        tab=tabLayout.newTab().setText("主播");
//        tabLayout.addTab(tab);
        //2.ViewPager加载
        pager = (ViewPager) view.findViewById(R.id.discover_view_pager);

        List<BaseFragment>fragments=new LinkedList<>();
        fragments.add(new DiscoverRecommendFragment());
        fragments.add(new DiscoverCategoryFragment());
        fragments.add(new DiscoverLiveFragment());
        fragments.add(new DiscoverRecommendFragment());
        fragments.add(new DiscoverAnchorFragment());
        CommonFragmentAdapter pagerAdatper=new CommonFragmentAdapter(getChildFragmentManager(),fragments);
       pager.setAdapter(pagerAdatper);
//        //ViewPager滑动与TabLayout绑定
//        pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//        tabLayout.setOnTabSelectedListener(this);

        //封装了TabLayout与ViewPager的联动
        //需要ViewPager内部指定的Adapter必须要重写getPageTitle()方法
        tabLayout.setupWithViewPager(pager);
        return view;
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        pager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
